<!-- 考勤统计 -->
<template>
  <a-card :bordered="false" class="table-page-search-wrapper">
    <a-form layout="inline" @keyup.enter.native="searchQuery">
     <a-row :gutter="24">
        <a-col :xl="6" :lg="7" :md="8" :sm="24" >
          <a-form-item label="合同编号">
            <a-input v-model="queryParam.code" />
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24" >
          <a-form-item label="合同状态">
            <j-dict-select-tag
              v-model="queryParam.contractStatus"
              placeholder="请选择"
              dict-code="xy_park_contract_status"
            >
            </j-dict-select-tag>
          </a-form-item>
        </a-col>
        <a-col :xl="6" :lg="7" :md="8" :sm="24" >
          <a-form-item>
            <a-button type="primary" icon="search" @click="searchQuery">查询</a-button>
            <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
          </a-form-item>
        </a-col>
     </a-row>
    </a-form>
    <vxe-grid
      id="ContractList"
      ref="vxeTable"
      v-bind="options"
      :columns="columns"
      :data="dataSource"
      highlight-current-row
      @toolbar-button-click="toolbarButtonClickEvent"
      @checkbox-all="onSelectChange"
      @checkbox-change="onSelectChange"
    >
      <template v-slot:toolbar_buttons>
        <a-dropdown>
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="handleAdd('1')">买卖合同</a-menu-item>
            <a-menu-item key="2" @click="handleAdd('2')">租赁合同</a-menu-item>
            <a-menu-item key="3" @click="handleAdd('3')">物业合同</a-menu-item>
          </a-menu>
          <a-button type="primary" icon="plus" >新增</a-button>
        </a-dropdown>
        <!-- <a-button style="margin-left:5px;" ghost type="primary" icon="import" @click="handleSub">提交</a-button>
        <a-button v-has="'ContractList:check'" style="margin-left:5px;" ghost type="primary" icon="check-square" @click="handleCheck">审批</a-button>
        <a-button v-has="'ContractList:reject'" style="margin-left:5px;" ghost type="danger" icon="check-square" @click="handleReject">驳回</a-button> -->
      </template>
      <template v-slot:operate="{ row }">
        <a v-if="row.contractStatus === '1'" @click="handleEdit(row)">编辑</a>
        <a-divider v-if="row.contractStatus === '1'" type="vertical" />
        <a @click="handleSub(row)" v-if="row.contractStatus === '1'">提交</a>
        <a-divider type="vertical" v-if="row.contractStatus === '1'" />
        <a @click="handleCheck(row)" v-has="'ContractList:check'" v-if="row.contractStatus === '2'">通过</a>
        <a-divider type="vertical" v-if="row.contractStatus === '2'" />
        <a @click="handleReject(row)" v-has="'ContractList:reject'" v-if="row.contractStatus === '2'">驳回</a>
        <a-divider type="vertical" v-if="row.contractStatus === '2'" />
        <a-dropdown>
          <a class="ant-dropdown-link">更多<a-icon type="down" /></a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a @click="handleDetail(row)">详情</a>
            </a-menu-item>
            <a-menu-item  v-if="row.contractStatus === '3'">
              <a @click="handleGenerateBill(row)">生成账单</a>
            </a-menu-item>
            <a-menu-item v-if="row.contractStatus === '1'" v-has="'OfficedocIssuedList:del'">
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(row.id)">
                <a>删除</a>
              </a-popconfirm>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </template>
      <template v-slot:pager>
        <vxe-pager
          :layouts="layouts"
          :current-page.sync="ipagination.currentPage"
          :page-size.sync="ipagination.pageSize"
          :total="ipagination.total"
          @page-change="handlePageChange"
        >
        </vxe-pager>
      </template>
    </vxe-grid>
    <ContractModal ref="modalForm" @ok="modalFormOk" />
    <ContractDetailModal ref="ContractDetailModal" />
  </a-card>
</template>
<script >
import { VxeTableMixin } from '@/mixins/VxeTableMixin'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import { putAction, getAction } from '@/api/manage'
import ContractModal from './modal/ContractModal'
import ContractDetailModal from './modal/ContractDetailModal'
export default {
  name: 'ContractList',
  mixins: [VxeTableMixin],
  components: {
    ContractModal,
    ContractDetailModal
  },
  data() {
    return {
      columns: [
        { type: 'checkbox', width: 50 },
        { type: 'seq', width: 60 },
        { field: 'contractCode', title: '合同编号', showOverflow: true },
        { field: 'contractDate', title: '合同日期', showOverflow: true },
        { field: 'contractType', title: '合同类型', slots: { default: ({ row }) => {
          return filterDictTextByCache('xy_park_contract_type', row.contractType)
        } }},
        { field: 'contractStatus', title: '合同审批状态', slots: { default: ({ row }) => {
          return filterDictTextByCache('xy_park_contract_status', row.contractStatus)
        } }},
        { field: 'bizUserEntity.realname', title: '跟进人' },
        { field: 'startDate', title: '合同期限', showOverflow: true,
          slots: { default: ({ row }) => {
            return (row.startDate || '') + '至' + (row.endDate || '')
          } }},
        { field: 'operate', title: '操作', width: 150, slots: { default: 'operate' }}
      ],
      url: {
        list: '/community/contract/list',
        delete: '/community/contract/delete',
        deleteBatch: '/community/contract/deleteBatch',
        approvalBatch: '/community/contract/approvalBatch',
        generateBill: '/community/contract/generateBill'
      },
      isorter: {
        column: '',
        order: ''
      }
    }
  },
  methods: {
    updateData(data) {
      putAction(this.url.edit, data).then(res => {
        if (res.success) {
          this.$message.success('审核成功')
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleDetail(data) {
      this.$refs.ContractDetailModal.show(data)
    },
    handleAdd(type) {
      this.$refs.modalForm.add(type)
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    handleCheck(row) {
      if (row.contractStatus === '2') {
        this.checkContract('3', row)
      } else {
        this.$message.warning('只能选择审批中状态的合同')
      }
      // if (this.selectionRows.length) {
      //   let flag = false
      //   this.selectionRows.forEach(item => {
      //     if (item.contractStatus !== '2') {
      //       flag = true
      //       return false
      //     }
      //   })
      //   if (flag) {
      //     return this.$message.warning('只能选择审批中状态的合同')
      //   }
      //   this.checkContract('3')
      // } else {
      //   this.$message.warning('请选择需要审批的合同')
      // }
    },
    handleSub(row) {
      if (row.contractStatus === '1') {
        this.checkContract('2', row)
      } else {
        this.$message.warning('只能选择未审批状态的合同')
      }
      // if (this.selectionRows.length) {
      //   let flag = false
      //   this.selectionRows.forEach(item => {
      //     if (item.contractStatus !== '1') {
      //       flag = true
      //       return false
      //     }
      //   })
      //   if (flag) {
      //     return this.$message.warning('只能选择未审批状态的合同')
      //   }
      //   this.checkContract('2')
      // } else {
      //   this.$message.warning('请选择需要提交审批的合同')
      // }
    },
    handleReject(row) {
      if (row.contractStatus === '2') {
        this.checkContract('1', row)
      } else {
        this.$message.warning('只能选择审批中状态的合同')
      }
      // if (this.selectionRows.length) {
      //   let flag = false
      //   this.selectionRows.forEach(item => {
      //     if (item.contractStatus !== '2') {
      //       flag = true
      //       return false
      //     }
      //   })
      //   if (flag) {
      //     return this.$message.warning('只能选择审批中状态的合同')
      //   }
      //   this.checkContract('1')
      // } else {
      //   this.$message.warning('请选择需要驳回的合同')
      // }
    },
    checkContract(status, row) {
      getAction(this.url.approvalBatch, { ids: row.id, status: status }).then(res => {
        if (res.success) {
          this.$message.success('操作成功')
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleGenerateBill(row) {
      getAction(this.url.generateBill, { contractCode: row.contractCode }).then(res => {
        if (res.success) {
          this.$message.success('账单生成成功')
          this.loadData()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less';

</style>
